package com.google.gsaguide.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gsaguide.MainActivity;
import com.google.gsaguide.R;

public class SummitFragment extends Fragment {

    // TODO: Need to optimize and comment this code.

    private static final String LOAD_URL = "http://felipeporge.com/scripts/gplus_tag.php?tag=gsasummit";
    private RelativeLayout mLoading;
    private WebView mWebView;
    private Context mContext;
    private int mTries = 0;

    public SummitFragment() {
    }

    public static SummitFragment newInstance() {
        SummitFragment fragment = new SummitFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_summit, container, false);
        mContext = getActivity().getApplicationContext();

        mLoading = (RelativeLayout) view.findViewById(R.id.summitLoading);

        mWebView = (WebView) view.findViewById(R.id.summitWebView);
        mWebView.setVisibility(View.INVISIBLE);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url.replaceAll("app/basic/", ""))));
                    return true;
                } else {
                    return false;
                }
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                mLoading.setVisibility(View.VISIBLE);
                mWebView.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mLoading.setVisibility(View.INVISIBLE);
                mWebView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                mLoading.setVisibility(View.VISIBLE);
                mWebView.setVisibility(View.INVISIBLE);
                Toast.makeText(mContext, errorCode + "Error.", Toast.LENGTH_SHORT).show();
                refreshPage(200);
            }
        });
        mWebView.loadUrl(LOAD_URL);
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(MainActivity.SUMMIT_FRAGMENT);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public void refreshPage(int delay) {
        if (mTries < 5) {
            mTries++;
            mWebView.loadUrl(LOAD_URL);
        } else {
            mWebView.setVisibility(View.INVISIBLE);
            mLoading.setVisibility(View.VISIBLE);
        }
    }

}
